{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2abc458d-52b5-410e-9c4d-eeb998d0ac02",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "学生成绩管理系统菜单:\n",
      "1. 记录学生成绩\n",
      "2. 查询学生成绩\n",
      "3. 统计成绩\n",
      "4. 退出系统\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入选项序号:  1\n",
      "请输入学生姓名:  Liang yi zhuo\n",
      "请输入学生学号:  202410139106\n",
      "请输入课程名称:  python\n",
      "请输入成绩 (0 - 100):  88\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "成绩记录成功。\n",
      "\n",
      "学生成绩管理系统菜单:\n",
      "1. 记录学生成绩\n",
      "2. 查询学生成绩\n",
      "3. 统计成绩\n",
      "4. 退出系统\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入选项序号:  2\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请选择查询方式:\n",
      "1. 按学生姓名查询\n",
      "2. 按学生学号查询\n",
      "3. 按课程名称查询\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入选项序号:  2\n",
      "请输入学生学号:  202410139106\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "查询结果如下:\n",
      "姓名: Liang yi zhuo, 学号: 202410139106, 课程: python, 成绩: 88.0\n",
      "\n",
      "学生成绩管理系统菜单:\n",
      "1. 记录学生成绩\n",
      "2. 查询学生成绩\n",
      "3. 统计成绩\n",
      "4. 退出系统\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入选项序号:  3\n",
      "请输入要统计的课程名称:  python\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python 课程的统计信息如下:\n",
      "平均分: 88.00\n",
      "最高分: 88.0\n",
      "最低分: 88.0\n",
      "\n",
      "学生成绩管理系统菜单:\n",
      "1. 记录学生成绩\n",
      "2. 查询学生成绩\n",
      "3. 统计成绩\n",
      "4. 退出系统\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入选项序号:  4\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "感谢使用，系统已退出。\n"
     ]
    }
   ],
   "source": [
    "students = []\n",
    "\n",
    "def add_grade():\n",
    "    while True:\n",
    "        name = input(\"请输入学生姓名: \")\n",
    "        student_id = input(\"请输入学生学号: \")\n",
    "        for student in students:\n",
    "            if student[\"student_id\"] == student_id:\n",
    "                print(\"错误: 该学号已存在，请重新输入。\")\n",
    "                break\n",
    "        else:\n",
    "            break\n",
    "    course = input(\"请输入课程名称: \")\n",
    "    while True:\n",
    "        try:\n",
    "            grade = float(input(\"请输入成绩 (0 - 100): \"))\n",
    "            if 0 <= grade <= 100:\n",
    "                break\n",
    "            else:\n",
    "                print(\"错误: 成绩必须在 0 到 100 分之间，请重新输入。\")\n",
    "        except ValueError:\n",
    "            print(\"错误: 输入的成绩不是有效的数字，请重新输入。\")\n",
    "    students.append({\n",
    "        \"name\": name,\n",
    "        \"student_id\": student_id,\n",
    "        \"course\": course,\n",
    "        \"grade\": grade\n",
    "    })\n",
    "    print(\"成绩记录成功。\")\n",
    "\n",
    "def query_grade():\n",
    "    print(\"请选择查询方式:\")\n",
    "    print(\"1. 按学生姓名查询\")\n",
    "    print(\"2. 按学生学号查询\")\n",
    "    print(\"3. 按课程名称查询\")\n",
    "    choice = input(\"请输入选项序号: \")\n",
    "    if choice == \"1\":\n",
    "        name = input(\"请输入学生姓名: \")\n",
    "        results = [student for student in students if student[\"name\"] == name]\n",
    "    elif choice == \"2\":\n",
    "        student_id = input(\"请输入学生学号: \")\n",
    "        results = [student for student in students if student[\"student_id\"] == student_id]\n",
    "    elif choice == \"3\":\n",
    "        course = input(\"请输入课程名称: \")\n",
    "        results = [student for student in students if student[\"course\"] == course]\n",
    "    else:\n",
    "        print(\"错误: 无效的选项，请重新选择。\")\n",
    "        return\n",
    "    if results:\n",
    "        print(\"查询结果如下:\")\n",
    "        for result in results:\n",
    "            print(f\"姓名: {result['name']}, 学号: {result['student_id']}, 课程: {result['course']}, 成绩: {result['grade']}\")\n",
    "    else:\n",
    "        print(\"未找到相关记录。\")\n",
    "\n",
    "def statistics_grade():\n",
    "    course = input(\"请输入要统计的课程名称: \")\n",
    "    grades = [student[\"grade\"] for student in students if student[\"course\"] == course]\n",
    "    if grades:\n",
    "        average = sum(grades) / len(grades)\n",
    "        highest = max(grades)\n",
    "        lowest = min(grades)\n",
    "        print(f\"{course} 课程的统计信息如下:\")\n",
    "        print(f\"平均分: {average:.2f}\")\n",
    "        print(f\"最高分: {highest}\")\n",
    "        print(f\"最低分: {lowest}\")\n",
    "    else:\n",
    "        print(\"未找到该课程的相关记录。\")\n",
    "\n",
    "def main():\n",
    "    while True:\n",
    "        print(\"\\n学生成绩管理系统菜单:\")\n",
    "        print(\"1. 记录学生成绩\")\n",
    "        print(\"2. 查询学生成绩\")\n",
    "        print(\"3. 统计成绩\")\n",
    "        print(\"4. 退出系统\")\n",
    "        choice = input(\"请输入选项序号: \")\n",
    "        if choice == \"1\":\n",
    "            add_grade()\n",
    "        elif choice == \"2\":\n",
    "            query_grade()\n",
    "        elif choice == \"3\":\n",
    "            statistics_grade()\n",
    "        elif choice == \"4\":\n",
    "            print(\"感谢使用，系统已退出。\")\n",
    "            break\n",
    "        else:\n",
    "            print(\"错误: 无效的选项，请重新输入。\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8efac38b-bd35-4014-8dc5-79566bffccb0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
